package com.czkt.shop.controller;

import com.czkt.shop.pojo.*;
import com.czkt.shop.service.IProductBrandService;
import com.czkt.shop.service.IProductSkuService;
import com.czkt.shop.service.IProductSpuService;
import com.czkt.shop.service.IShopCartService;
import com.czkt.shop.utils.RedisTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shopCart")
@CrossOrigin
public class ShopCartController {

    @Autowired
    IShopCartService shopCartService;

    @Autowired
    IProductSkuService productSkuService;

    @Autowired
    IProductSpuService productSpuService;

    @Autowired
    IProductBrandService productBrandService;

    @Autowired
    RedisTokenUtils redisTokenUtils;

    /**
     * 返回购物车信息
     * @return
     */
    @RequestMapping("/cart/list")
    public Object list(){
//        String token

//        Integer uid = redisTokenUtils.token(token);
        Integer uid = 1;

        List<ShopCart> shopCartList = shopCartService.list(uid.longValue());

        Set<Long> shopSet = shopCartList.stream().map(item->{
            return item.getSkuId();
        }).collect(Collectors.toSet());

        List<ProductSku> skuList = productSkuService.list(shopSet);

        Set<Long> skuSet = skuList.stream().map(item->{
            return item.getSpuId();
        }).collect(Collectors.toSet());

        List<ProductSpu> spuList = productSpuService.list(skuSet);

        Set<Long> brandSet = spuList.stream().map(item->{
            return item.getTrandId();
        }).collect(Collectors.toSet());

        List<ProductBrand> brandList = productBrandService.list(brandSet);

        Map<String,Object> reslutMap = new HashMap<>();
        reslutMap.put("productSku",skuList);
        reslutMap.put("productBrand",brandList);
        reslutMap.put("productSpu",spuList);

        return reslutMap;
    }

    @RequestMapping("/cartCount")
    public Object count(String token){
        Integer uid = redisTokenUtils.token(token);
        return shopCartService.count(uid);
    }

    @RequestMapping("/add")
    public Object add(ShopCart shopCart, String token){

        System.out.println("shop" + shopCart.getSkuId());

        Integer uid = redisTokenUtils.token(token);
        shopCart.setUserId(uid);
        Integer result;
        ShopCart shopCart1 = shopCartService.selectOne(shopCart.getSkuId());

//        System.out.println("修改是否存在" + shopCart1.getCartId());

        if (shopCart1 == null){
            System.out.println("添加");
            result = shopCartService.save(shopCart)?1:0;
        } else {
            System.out.println("修改");
            result = shopCartService.updateById(shopCart1)?1:0;
        }


        return result;
    }
}